package org.example.thread.futuretask.demo2;

import java.util.concurrent.*;

/**
 * 计算阶✖️10
 */
public class test{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        单线程方法
//        FutureTask<Integer> futureTask=new FutureTask(new Factorial(10));
//
//        Thread t=new Thread(futureTask);
//        t.start();
//        //调用get方法获取线程计算的返回值，调用get会阻塞
//        System.out.println(futureTask.get());

        //创建一个线程池方法
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        //线程池传递一个实现Callable接口的实现类
        Future<Integer> submit = executorService.submit(new Factorial(10));

        System.out.println(submit.get());
        executorService.shutdown();//关闭线程池


    }
   static class Factorial implements Callable<Integer> {
        private int n;

       public Factorial(int n) {
           this.n = n;
       }

       @Override
        public Integer call() throws Exception {
            if(n==0){
                return 1;
            }
            int num=n;
            for (int i = 1; i < n; i++) {
                num*=i;
            }
            return num;
        }
    }
}

